<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);

// Basic parameter data
$mode		= request_var('mode', '');

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id == ANONYMOUS)
{
	sv_login_box();
}

if ($mode == 'buy')
{	
	$corp_id	= $db->sql_escape(request_var('corp_id', ''));
	$number		= $db->sql_escape(request_var('number', ''));
	$share		= (int)($number) * 1000;
	$note		= $db->sql_escape(utf8_clean_string((request_var('note', '', true))));
	
	$request_price	= $db->sql_escape(request_var('request_price', ''));	
	
	if (is_valid_corp_id($corp_id) && $share > 0)
	{
		$sql = 'INSERT INTO sv_transaction ' . $db->sql_build_array('INSERT', array(
				'user_id'		=> (int) $user->data['user_id'],
				'corp_id'		=> $corp_id,
				'action'		=> 'to_buy',
				'share'			=> (int) $share,
				'price'			=> (float) 0.0,
				'request_price'	=> $request_price,
				'request_time'	=> get_taiwan_time(),
				'note'			=> $note,
				'deal_time'		=> '')
			);
	
		$db->sql_query($sql);
	
		$template->assign_vars(array(
	
			'PREVIOUS_CORP_ID'				=> $corp_id,
			'PREVIOUS_NUMBER'				=> $number,
			));
	}
}
else if ($mode == 'delete_delegation')
{		
	$trans_id	= (int)(request_var('trans_id', ''));
	
	delete_delegation($trans_id);
}

// list hot stocks.
$hot_corp_list = list_hot_corp(32);

$corps_may_interest_list = list_corps_may_interest($user_id, 32);

// list all open to_buy delegation. 
list_open_to_buy_delegation();


display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(
	
	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'						=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_BUY_ACTION'							=> append_sid("{$phpbb_root_path}buy.$phpEx", 'mode=buy'),
	'S_DELETE_TO_BUY_DELEGATION_ACTION'		=> append_sid("{$phpbb_root_path}buy.$phpEx", 'mode=delete_delegation'),
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'S_TO_BUY_DELEGATION_LIST'	=> $to_buy_delegation_list,
	'S_TO_SELL_DELEGATION_LIST'	=> $to_sell_delegation_list,
	'S_HOLDING_STOCK_LIST'		=> $holding_stock_list,
	'S_HOT_CORP_LIST'			=> $hot_corp_list,		
	'S_CORPS_MAY_INTEREST'		=> $corps_may_interest_list,
	
	'S_ALLOW_DELETE_TO_BUY_DELEGATION'		=> true,	
	
	'U_MONEY'			=> number_format($money),
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '',
	
	'S_INCLUDE_JQUERY'				=> true,				// to instruct sv_overall_header.html to include jQuery. 
	'S_INCLUDE_JQUERY_UI'			=> true,				// to instruct sv_overall_header.html to include jQuery UI. 
	
	'ALLOW_AD'						=> is_allow_ad($user_id),
	) 
);



// Output page
page_header($user->lang['SV_BUY']);

$template->set_filenames(array(
	'body' => 'buy.html')
);

page_footer();

?>